package com.crm.Servlet;

import com.crm.Service.VisiterService;
import com.crm.ServiceImpl.VisiterServiceImpl;
import com.crm.pojo.Visiter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @version : 1.0
 * @auther : hjx
 * @Date : 2021/6/30
 * @Description : 实训
 */
public class updateVisitServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        int visitId = Integer.parseInt(req.getParameter("visitId"));
        String custName = req.getParameter("custName");
        int custId = Integer.parseInt(custName.split("-")[0]);
        String user = req.getParameter("principal");
        int principal = Integer.parseInt(user.split("-")[0]);

        Date visitTime = null;
        Date visitNextTime = null;
        try {
            visitTime = dateFormat.parse(req.getParameter("visitTime"));
            visitNextTime = dateFormat.parse(req.getParameter("visitNextTime"));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String visitInterviewee = req.getParameter("visitInterviewee");
        String visitAddr = req.getParameter("visitAddr");
        String visitDetail = req.getParameter("visitDetail");

        Visiter visiter = new Visiter(visitId,custId,principal,visitTime,visitInterviewee,visitAddr,visitDetail,visitNextTime);
        VisiterService visiterService = new VisiterServiceImpl();
        visiterService.updateVisit(visiter);

        req.getRequestDispatcher("allVisiter").forward(req,resp);

    }


}

